package com.haixiaoke.saas.messageCenter.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.enums.PushConditionEnum;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.department.domain.Tenant;
import com.haixiaoke.saas.department.service.ITenantService;
import com.haixiaoke.saas.messageCenter.domain.PushCondition;
import com.haixiaoke.saas.messageCenter.mapper.PushConditionMapper;
import com.haixiaoke.saas.messageCenter.service.IPushConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 推送条件信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-11-11
 */
@Service
public class PushConditionServiceImpl implements IPushConditionService {
    @Autowired
    private PushConditionMapper pushConditionMapper;
    @Autowired
    private ITenantService tenantService;
    /**
     * 查询推送条件信息
     *
     * @param pushConditionId 推送条件信息主键
     * @return 推送条件信息
     */
    @Override
    public PushCondition selectPushConditionByPushConditionId(Long pushConditionId) {
        return pushConditionMapper.selectPushConditionByPushConditionId(pushConditionId);
    }

    /**
     * 查询推送条件信息列表
     *
     * @param pushCondition 推送条件信息
     * @return 推送条件信息
     */
    @Override
    public List<PushCondition> selectPushConditionList(PushCondition pushCondition) {
        return pushConditionMapper.selectPushConditionList(pushCondition);
    }

    /**
     * 新增推送条件信息
     *
     * @param pushCondition 推送条件信息
     * @return 结果
     */
    @Override
    public int insertPushCondition(PushCondition pushCondition) {
        Tenant tenant = tenantService.selectDepByDepId(pushCondition.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            throw new ServiceException("机构不存在");
        }
        pushCondition.setPushConditionCode(PushConditionEnum.getCodeByValue(pushCondition.getConditionType()));
        pushCondition.setTenantName(tenant.getTenantName());
        pushCondition.setCreateTime(DateUtils.getNowDate());
        return pushConditionMapper.insertPushCondition(pushCondition);
    }

    /**
     * 修改推送条件信息
     *
     * @param pushCondition 推送条件信息
     * @return 结果
     */
    @Override
    public int updatePushCondition(PushCondition pushCondition) {
        Tenant tenant = tenantService.selectDepByDepId(pushCondition.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            throw new ServiceException("机构不存在");
        }
        pushCondition.setPushConditionCode(PushConditionEnum.getCodeByValue(pushCondition.getConditionType()));
        pushCondition.setTenantName(tenant.getTenantName());
        pushCondition.setUpdateTime(DateUtils.getNowDate());
        return pushConditionMapper.updatePushCondition(pushCondition);
    }

    /**
     * 批量删除推送条件信息
     *
     * @param pushConditionIds 需要删除的推送条件信息主键
     * @return 结果
     */
    @Override
    public int deletePushConditionByPushConditionIds(Long[] pushConditionIds) {
        return pushConditionMapper.deletePushConditionByPushConditionIds(pushConditionIds);
    }

    /**
     * 删除推送条件信息信息
     *
     * @param pushConditionId 推送条件信息主键
     * @return 结果
     */
    @Override
    public int deletePushConditionByPushConditionId(Long pushConditionId) {
        return pushConditionMapper.deletePushConditionByPushConditionId(pushConditionId);
    }

}
